﻿using System.Collections.Generic;
using System.Text;

namespace CssCommonLibrary
{
    public class CssNodeList : List<CssClassNode>
    {
        
        #region Constructors
        public CssNodeList() { }

        public CssNodeList(CssNodeList list)
        {
            this.AddRange(list);
        }

        public CssNodeList(List<CssClassNode> list)
        {
            this.AddRange(list);
        }

        public CssNodeList(IList<CssClassNode> list)
        {
            this.AddRange(list);
        }
        #endregion

        public new void Add(CssClassNode node)
        {
            CssClassNode availableNode = this.GetNodeBySelector(node.Selector);
            if (availableNode != null)
            {
                availableNode += node;
            }
            else
            {
                base.Add(node);
            }
        }

        public CssClassNode GetNodeBySelector(string selector)
        {
            return this.Find(t => t.Selector == selector);
        }

        public override string ToString()
        {
            StringBuilder strBuilder = new StringBuilder();
            foreach (var node in this)
                strBuilder.AppendLine(node.ToString());
            return strBuilder.ToString();
        }
    }
}
